<?php

	require('../../database/bd_connect.php');

	if (isset($_GET['method'])){	
		$method 	= 	$_GET['method'] 	;
		$city 		= 	$city = iconv('utf-8', 'windows-1252', $_GET['city']); 		;
		$access 	= 	$_GET['access'] 	;
		$parameter 	= 	$_GET['parameter'] 	;
		$condition 	= 	$_GET['condition'] 	;
		$value 		= 	$_GET['value'] 		;
		$fake 		= 	$_GET['fake'] 		;

		if ($method=="Quality"){
			
			if($value  == "Normal") 		{ $value  = 3000	;	$value2 = 5000 	;	}
			if($value  == "Strong") 		{ $value  = 5000 	;	$value2 = 7000 	;	}
			if($value  == "Heavy") 			{ $value  = 7000 	;	$value2 = 9000 	;	}
			if($value  == "Extreme Heavy") 	{ $value  = 9000 	;	$value2 = 12000 ;	}
			if($value  == "Idle") 			{ $value  = 0 		;	$value2 = 3000 	;	}

				if($condition == "=")   $sql2 = ">=".$value." AND abs(noise_hour.average)<".$value2 ; 
				else 					$sql2 = $condition.$value ;			
			
				if($city == "Portugal" && $access != "All"){
					$sql = "select DATE_FORMAT(noise_hour.time, '%M %d') as TIME , cpe.city as CITY, cpe.access as ACCESS , cpe.mac as MAC , cpe.wlan_channel as CHANNEL, cpe.cpe_id as CPE , ROUND(abs(noise_hour.average)) as NOISE from cpe inner join noise_hour on cpe.wlan_channel=noise_hour.channel AND  cpe.cpe_id=noise_hour.cpe_id  WHERE cpe.wlan_channel=noise_hour.channel AND abs(noise_hour.average)".$sql2." AND time >= NOW() - INTERVAL 4 DAY AND cpe.access='".$access."' AND cpe.cpe_data='".$fake."' GROUP by cpe.mac";
				}
				elseif($city != "Portugal" && $access == "All"){
					$sql = "select DATE_FORMAT(noise_hour.time, '%M %d') as TIME , cpe.city as CITY, cpe.access as ACCESS , cpe.mac as MAC , cpe.wlan_channel as CHANNEL, cpe.cpe_id as CPE , ROUND(abs(noise_hour.average)) as NOISE from cpe inner join noise_hour on cpe.wlan_channel=noise_hour.channel AND  cpe.cpe_id=noise_hour.cpe_id WHERE cpe.wlan_channel=noise_hour.channel AND abs(noise_hour.average)".$sql2." AND cpe.cpe_data='".$fake."' AND time >= NOW() - INTERVAL 4 DAY AND cpe.city='".$city."' GROUP by cpe.mac";
				}
				elseif($city == "Portugal" && $access == "All"){
					$sql = "select DATE_FORMAT(noise_hour.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS , cpe.mac as MAC , cpe.wlan_channel as CHANNEL, cpe.cpe_id as CPE , ROUND(abs(noise_hour.average)) as NOISE from cpe inner join noise_hour on cpe.wlan_channel=noise_hour.channel AND  cpe.cpe_id=noise_hour.cpe_id  WHERE abs(noise_hour.average)".$sql2." AND cpe.cpe_data='".$fake."' AND time >= NOW() - INTERVAL 4 day GROUP by cpe.mac";
				}
				else{
					$sql = "select DATE_FORMAT(noise_hour.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS , cpe.mac as MAC , cpe.wlan_channel as CHANNEL, cpe.cpe_id as CPE , ROUND(abs(noise_hour.average)) as NOISE from cpe inner join noise_hour on cpe.wlan_channel=noise_hour.channel AND  cpe.cpe_id=noise_hour.cpe_id  WHERE abs(noise_hour.average)".$sql2." AND time >= NOW() - INTERVAL 4 DAY AND cpe.city='".$city."' AND cpe.access='".$access."' AND cpe.cpe_data='".$fake."' GROUP by cpe.mac";
				}
				//echo $sql;
				$result = $conn->query($sql);
				echo 	'<div class="panel panel-default">
						<table class="table grid table-striped table-hover">
						<tr>
					    <th>Time</th>
					    <th>City</th>
					    <th>Access</th>
					    <th>MAC Address</th> 
					    <th>Channel</th>
					    <th>Noise</th>
					    <th> </th>
					  	</tr>';				
				while ($row = mysqli_fetch_array($result)) {
					echo "<tr>";
					echo "<td>";
					echo $row['TIME'];
					echo "</td>";
					echo "<td>";
					echo iconv('windows-1252', 'utf-8', $row['CITY']);
					echo "</td>";
					echo "<td>";
					echo $row['ACCESS'];
					echo "</td>";
					echo "<td>";
					echo $row['MAC'];
					echo "</td>";
					echo "<td>";
					echo $row['CHANNEL'] ;
					echo "</td>";
					echo "<td>";
					if( $row['NOISE'] >= 0 		&& $row['NOISE'] < 3000	) 	echo "Idle (".$row['NOISE'].")" 			;
					if( $row['NOISE'] >= 3000 	&& $row['NOISE'] < 5000	) 	echo "Normal (".$row['NOISE'].")" 			;
					if( $row['NOISE'] >= 5000 	&& $row['NOISE'] < 7000	) 	echo "Strong (".$row['NOISE'].")" 			;
					if( $row['NOISE'] >= 7000 	&& $row['NOISE'] < 9000	)	echo "Heavy (".$row['NOISE'].")" 			;
					if( $row['NOISE'] >= 9000 	&& $row['NOISE'] < 12000) 	echo "Extreme Heavy (".$row['NOISE'].")" 	;
					echo "</td>";
					echo "<td><button class='btn btn-default' onclick='selectcpe(".$row['CPE'].");'>";
					echo "View";
					echo "</button></td>";
					echo "</tr>";
				}
				echo 	'</table>
						</div>';
			}
		if ($method=="Churn"){ 
				if($city == "Portugal" && $access != "All"){
					$sql = "select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.cpe_id as CPE, cpe.mac as MAC, statistics.vodafone_churn as Vodafone_Churn, statistics.pt_churn as PT_Churn, statistics.nos_churn as NOS_Churn FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where cpe.access='".$access."' and statistics.vodafone_churn".$condition."".$value."  AND date(`time`)= CURDATE() AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time asc";
				}
				elseif($city != "Portugal" && $access == "All"){
					$sql = "select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.cpe_id as CPE, cpe.mac as MAC, statistics.vodafone_churn as Vodafone_Churn, statistics.pt_churn as PT_Churn, statistics.nos_churn as NOS_Churn FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where cpe.city='".$city."' and statistics.vodafone_churn".$condition."".$value."  AND date(`time`)= CURDATE() AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time asc";
				}
				elseif($city == "Portugal" && $access == "All"){
					$sql = "select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.cpe_id as CPE, cpe.mac as MAC, statistics.vodafone_churn as Vodafone_Churn, statistics.pt_churn as PT_Churn, statistics.nos_churn as NOS_Churn FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where statistics.vodafone_churn".$condition."".$value." AND date(`time`)= CURDATE() AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time asc";
				}
				else{
					$sql = "select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.cpe_id as CPE, cpe.mac as MAC, statistics.vodafone_churn as Vodafone_Churn, statistics.pt_churn as PT_Churn, statistics.nos_churn as NOS_Churn FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where cpe.city='".$city."' and cpe.access='".$access."' and statistics.vodafone_churn".$condition."".$value."  AND date(`time`)= CURDATE()  AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time asc";
				}
				//echo $sql;
				$result = $conn->query($sql);
				echo 	'<div class="panel panel-default">
						<table class="table table-striped table-hover">
						<tr>
					    <th>Time</th>
					    <th>City</th>
					    <th>Access</th>
					    <th>MAC Address</th> 
					    <th>Vodafone Churn</th>
					    <th>PT Churn</th>
					    <th>NOS Churn</th>
					    <th> </th>
					  	</tr>';
				while ($row = mysqli_fetch_array($result)) {
					echo "<tr>";
					echo "<td>";
					echo $row['TIME'];
					echo "</td>";
					echo "<td>";
					echo iconv('windows-1252', 'utf-8', $row['CITY']);
					echo "</td>";
					echo "<td>";
					echo $row['ACCESS'];
					echo "</td>";
					echo "<td>";
					echo $row['MAC'];
					echo "</td>";
					echo "<td>";
					if($row['Vodafone_Churn'] < 0 ) 	echo "0" . " %";
					else 								echo $row['Vodafone_Churn'] . " %";
					echo "</td>";
					echo "<td>";
					if($row['PT_Churn'] < 0 ) 			echo "0" . " %";
					else 								echo $row['PT_Churn'] . " %";
					echo "</td>";
					echo "<td>";
					if($row['NOS_Churn'] < 0 ) 			echo "0" . " %";
					else 								echo $row['NOS_Churn'] . " %";
					echo "</td>";
					echo "<td><button class='btn btn-default' onclick='selectcpe(".$row['CPE'].");'>";
					echo "View";
					echo "</button></td>";
					echo "</tr>";
				}
				echo 	'</table>
						</div>';
		}
		if ($method=="Share"){
				if($city == "Portugal" && $access != "All"){
					$sql = "Select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.mac as MAC, cpe.cpe_id as CPE, statistics.vodafone_market as Vodafone_Market, statistics.pt_market as PT_Market, statistics.nos_market as NOS_Market, statistics.others_market as Others_Market FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where cpe.access='".$access."' and statistics.vodafone_market".$condition."".$value." AND date(`time`)= CURDATE() AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time desc";
				}
				elseif($city != "Portugal" && $access == "All"){
					$sql = "Select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.mac as MAC, cpe.cpe_id as CPE, statistics.vodafone_market as Vodafone_Market, statistics.pt_market as PT_Market, statistics.nos_market as NOS_Market, statistics.others_market as Others_Market FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where cpe.city='".$city."' and statistics.vodafone_market".$condition."".$value." AND date(`time`)= CURDATE() AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time desc";
				}
				elseif($city == "Portugal" && $access == "All"){
					$sql = "Select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.mac as MAC, cpe.cpe_id as CPE, statistics.vodafone_market as Vodafone_Market, statistics.pt_market as PT_Market, statistics.nos_market as NOS_Market, statistics.others_market as Others_Market FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where statistics.vodafone_market".$condition."".$value." AND date(`time`)= CURDATE() AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time desc";
				}
				else{
					$sql = "Select  DATE_FORMAT(statistics.time, '%M %d') as TIME, cpe.city as CITY, cpe.access as ACCESS, cpe.mac as MAC, cpe.cpe_id as CPE, statistics.vodafone_market as Vodafone_Market, statistics.pt_market as PT_Market, statistics.nos_market as NOS_Market, statistics.others_market as Others_Market FROM cpe inner join statistics ON statistics.cpe_id=cpe.cpe_id where cpe.city='".$city."' and cpe.access='".$access."' and statistics.vodafone_market".$condition."".$value." AND date(`time`)= CURDATE() AND cpe.cpe_data='".$fake."' group by cpe.mac order by cpe.cpe_id,time desc";
				}
			$result = $conn->query($sql);
				echo 	'<div class="panel panel-default">
						<table class="table table-striped table-hover">
						<tr>
					    <th>Time</th>
					    <th>City</th>
					    <th>Access</th>
					    <th>MAC Address</th> 
					    <th>Vodafone Market</th>
					    <th>PT Market</th>
					    <th>NOS Market</th>
					    <th> </th>
					  	</tr>';			
					  	while ($row = mysqli_fetch_array($result)) {
							echo "<tr>";
							echo "<td>";
							echo $row['TIME'];
							echo "</td>";
							echo "<td>";
							echo iconv('windows-1252', 'utf-8', $row['CITY']);
							echo "</td>";
							echo "<td>";
							echo $row['ACCESS'];
							echo "</td>";
							echo "<td>";
							echo $row['MAC'];
							echo "</td>";
							echo "<td>";
							echo $row['Vodafone_Market'] . " %";
							echo "</td>";
							echo "<td>";
							echo $row['PT_Market'] . " %";
							echo "</td>";
							echo "<td>";
							echo $row['NOS_Market'] . " %";
							echo "</td>";
							//echo "<td>";
							//echo $row['Others_Market'] . " %";
							//echo "</td>";
							echo "<td><button class='btn btn-default' onclick='selectcpe(".$row['CPE'].");'>";
							echo "View";
							echo "</button></td>";
							echo "</tr>";
						}
						echo 	'</table>
						</div>';
		}
	}
	else{
		echo "error";
	}
	mysqli_close($conn);
//echo "end" ;
?>